<script type="text/javascript" src="j/pagination/jquery.pagination.js"></script>
<script type="text/javascript" src="j/replycontrols.js"></script>
<script type="text/javascript"> 
	
		function pageselectCallback(page_index, jq){
				$('#Searchresult').fadeOut(500, function(){
					var new_content = $('#hiddenresult div.result:eq('+page_index+')').clone();
					$('#Searchresult').empty().append(new_content);
					bindControls();
					$('#Searchresult').slideDown(500);
				})
			return false;
		}
	   
		function initPagination() {
			var num_entries = $('#hiddenresult div.result').length;
			// Create pagination element
			$("#Pagination").pagination(num_entries, {
				num_edge_entries: 2,
				num_display_entries: 8,
				callback: pageselectCallback,
				items_per_page:1
			});
		 }
	 
		function bindControls(){
			
			//hover states on the static widgets
			$('.reply_link_box, .new_topic_box, .delete_reply, .edit_reply, .sticky_thread, .quote_box').hover(
				function() { $(this).addClass('ui-state-hover'); }, 
				function() { $(this).removeClass('ui-state-hover'); }
			)
			
			$('.delete_reply').bind('click', function(){
				if($(this).attr('dd')=="deleted"){
					postId = $(this).attr('tt');
					topicId = $('#reply_topic').val();
					undeletePost(postId,topicId);
				} else {
					postId = $(this).attr('tt');
					topicId = $('#reply_topic').val();
					deletePost(postId,topicId);
				}
			});
			
			$('.sticky_thread').bind('click', function(){
				topicId=$(this).attr('tt');
				if($(this).attr('dd')=="stuck"){
					stickTopic("unstick", topicId)
				} else {
					stickTopic("stick", topicId)
				}
			});
			
			bindReplyControls();
			
		}
	
		function deletePost(postId, topicId){
			var outcome = confirm("Are you sure you want to delete this post?");
			if(outcome){
				$.get('ajax/forum/mod.php', { type: 'deletePost', postid: postId, topicid: topicId }, function(data){
					if(data=="ok"){
						$('#post'+postId).removeClass();
						$('#post'+postId).addClass("post_deleted ui-corner-all");
						$('#delete_reply_'+postId).attr('title', "Un-delete this reply")
						$('#delete_reply_'+postId).unbind();
						$('#delete_reply_'+postId).bind('click', function(){
							postId = $(this).attr('tt');
							topicId = $('#reply_topic').val();
							undeletePost(postId,topicId);											  
						});
					} else {
						alert(data);	
					}					
				});
			}
		}

		function undeletePost(postId, topicId){
			var outcome = confirm("Are you sure you want to undelete this post?");
			if(outcome){
				$.get('ajax/forum/mod.php', { type: 'undeletePost', postid: postId, topicid: topicId }, function(data){
					if(data=="ok"){
						$('#post'+postId).removeClass();
						$('#post'+postId).addClass("post ui-corner-all");
						$('#delete_reply_'+postId).attr('title', "Delete this reply")
						$('#delete_reply_'+postId).unbind();
						$('#delete_reply_'+postId).bind('click', function(){
							postId = $(this).attr('tt');
							topicId = $('#reply_topic').val();
							deletePost(postId,topicId);											  
						});
					} else {
						alert(data);	
					}					
				});
			}
		}
		
		function stickTopic(direction, topicId){
			$.get('ajax/forum/mod.php', { type: direction, topicid: topicId }, function(data){
				if(data=="ok"){
					if(direction == 'stick'){
						newText = 'unsticky thread'
						$('.sticky_thread').attr('dd', 'stuck')
					} else {
						newText = 'sticky thread'
						$('.sticky_thread').attr('dd', 'unstuck')
					}
					$('.sticky_thread').html(newText)
					$('.sticky_thread').unbind();
					$('.sticky_thread').bind('click', function(){
						  topicId=$(this).attr('tt');
						  if($(this).attr('dd')=="stuck"){
							  stickTopic("unstick", topicId)
						  } else {
							  stickTopic("stick", topicId)
						  }
					});
					
				} else {
					alert(data);	
				}
			});
		}
	
	$(document).ready(function () {
								
		initPagination();
		$('.breadlink').bind('click', function(){
		  var Anchor = $(this).attr("href");
		  if(Anchor.match('#')){
			  if(Anchor.match(':')){
				  args = Anchor.split(':')[1]
				  Anchor = Anchor.split(':')[0]
			  } else {
				  args = null;  
			  }
			  switch(Anchor){
				  case '#forum':
					fetchPage('forum', args)
				  break;
				  case '#board':
					fetchPage('board', args)
				  break;
				  case '#topic':
					fetchPage('topic', args)
				  break;
				  case '#reply':
					fetchPage('reply', args)
				  break;
			  }
		  }
		});
		
		bindControls();
		
		$('#reply_box_container').hide();
		
		reassert('.proflinks');
		
	});
</script>
<div id="reply_box_container"></div>
<input name="reply_box_active" id="reply_box_active" type="hidden" value="0" />
<input name="reply_topic" id="reply_topic" type="hidden" value="{{topic_id}}" />
<input name="reply_category" id="reply_category" type="hidden" value="{{category_id}}" />
<input name="reply_board" id="reply_board" type="hidden" value="{{board_id}}" />
<input name="reply_title" id="reply_title" type="hidden" value="{{topic_text}}" />
<div class="topic_posts">
	<div id="breadbasket">
        <div class="breadcrumbs"><a class="breadlink home_icon" href="#forum">Forums</a> > <a class="breadlink" id="no_highlight" href="{{category_link}}">{{category_text}}</a> > <a class="breadlink" id="no_highlight" href="{{board_link}}">{{board_text}}</a> > <a class="breadlink" id="no_highlight" href="{{topic_link}}">{{topic_text}}</a></div>
    </div>
    <div class="thread_control"><div class="reply_link_box{{locked_tag}} ui-state-default ui-corner-all">{{reply_text}}</div><div class="new_topic_box ui-state-default ui-corner-all">start new topic</div></div>
	<div id="Pagination" class="pagination"></div>
    <br style="clear:both;" />	
	<div id="Searchresult">
	</div>
  
	<div id="hiddenresult" style="display:none;">
		<div class="result">
			{{topic_posts}}
		</div>
	</div>
	<div id="Pagination" class="pagination">
	</div>
	<br style="clear:both;" />	
                    
</div>
<div class="thread_control"><div class="reply_link_box{{locked_tag}} ui-state-default ui-corner-all">{{reply_text}}</div><div class="new_topic_box ui-state-default ui-corner-all">start new topic</div></div>
